const conn = require('../../data/index');
const fs = require('fs')
// 
// 显示顶部轮播页面
module.exports.BeBannerShow = (req, res) => {
    res.render('background/banner_top')
}
// 显示顶部轮播拉取数据
module.exports.shwoTopBanner = (req, res) => {
    conn.query('select * from top_carousel', (err, data) => {
        if (err) return console.log(err);
        res.json(data)
    })
}
// 顶部轮播图修改图片
module.exports.upBannerImg = (req, res) => {
    fs.writeFile('./public/upload/' + req.file.originalname, req.file.buffer, (error) => {
        if (error) {
            return console.log(error);
        }

        res.json({
            code: '11101',
            pic: '/upload/' + req.file.originalname
        })
    })
}
// 顶部轮播图修改图片后传数据库
module.exports.pushBannerImg = (req, res) => {
    // UPDATE top_carousel set image_location=111 where id = 1
    conn.query("update top_carousel set site=? where id = ?", [req.body.mas, req.body.keys], (err, data) => {
        if (err) return console.log(err);
        res.json({
            code: '11102',
            mas: '更新成功'
        })
    })
}

// 店铺展示页面
module.exports.storeBanner = (req, res) => {
    res.render('background/banner_storShow')
}
// 首屏拉取数据
module.exports.showStoreBanner = (req, res) => {
    conn.query('select * from show_carousel',(err,data)=>{
        if(err) return console.log(err);
        res.json(data)
    })
}

// 点击更换图片
module.exports.upBannerStore = (req,res)=>{
    fs.writeFile('./public/upload/' + req.file.originalname, req.file.buffer, (error) => {
        if (error) {
            return console.log(error);
        }

        res.json({
            code: '21101',
            pic: '/upload/' + req.file.originalname
        })
    })
}
// 店铺修改图片上传
module.exports.pushBannerStore = (req,res)=>{
    conn.query("update show_carousel set site=? where id = ?", [req.body.mas, req.body.keys], (err, data) => {
        if (err) return console.log(err);
        res.json({
            code: '21102',
            mas: '更新成功'
        })
    })
}

// 加盟优势
module.exports.joinBanner =(req,res)=>{
    res.render('background/banner_join')
}
// 首屏数据
module.exports.showJoinBanner = (req,res)=>{
    conn.query('select * from well_carousel',(err,data)=>{
        if(err) return console.log(err);
        res.json(data)
    })
}
// 修改上传图片
module.exports.upImgJoin = (req,res)=>{
    fs.writeFile('./public/upload/' + req.file.originalname, req.file.buffer, (error) => {
        if (error) {
            return console.log(error);
        }

        res.json({
            code: '31101',
            pic: '/upload/' + req.file.originalname
        })
    })
}
module.exports.pushBannerJoin = (req,res)=>{
    conn.query("update well_carousel set site=? where id = ?", [req.body.mas, req.body.keys], (err, data) => {
        if (err) return console.log(err);
        res.json({
            code: '31102',
            mas: '更新成功'
        })
    })
}
module.exports.pushOfficJoin = (req,res)=>{
    console.log(req.body);
    conn.query('update well_carousel set title=?,content=? where id = ?',[req.body.title,req.body.conent,req.body.id],(err,data)=>{
        if(err) return console.log(err);
        res.json({
            code:'41101',
            mas:'修改成功'
        })
    })
}